package com.zhongen.wflow.workflow.service.impl;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zhongen.admin.api.util.HttpClientUtil;
import com.zhongen.quartz.util.WxProperties;
import com.zhongen.common.security.service.PigUser;
import com.zhongen.common.security.util.SecurityUtils;
import com.zhongen.wflow.workflow.service.WxCommonService;
import com.zhongen.wflow.mapper.WxCommonMapper;
import com.zhongen.quartz.util.WeChatSubscribeMessageSender;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class WxCommonServiceImpl implements WxCommonService {

	@Autowired
	WxCommonMapper wxCommonMapper;

	private final WxProperties wxProperties;

	@Override
	public String sendMsgs(String instanceId,String result,String name,String time,String content){
		List<String> openidList = wxCommonMapper.selectOpenidByExampleId(instanceId);
		for (String openid : openidList) {
			sendMsg(openid,result,name,time,content);
		}
		return "";
	}

	@Override
	public String subMsg(String exampleId) {
		PigUser loginUser = SecurityUtils.getUser();
		String openid = wxCommonMapper.getOpenid(loginUser.getId());
		wxCommonMapper.subMsg(openid,exampleId);
		return "添加成功";
	}

	public void sendMsg(String openid,String result,String name,String time,String content){
		String appid = wxProperties.getAppid();
		String appSecret = wxProperties.getSecret();
//			String openid = "o1_mB6ysGVP4XnxZdmrFIM_94wGE";
		String templateId = wxProperties.getTemplateId();
		String templateUrl = wxProperties.getTemplateUrl();

		//String token = request.getParameter("token");
		//请求 微信接口 获取 accessToken
		String accessToken = refreshAccessToken(appid,appSecret);
		// 构建订阅消息内容的JSON对象
		JSONObject messageData = new JSONObject();
		messageData.put("phrase1", createDataItem("审批结果", result));
		messageData.put("name2", createDataItem("审批人", name));
		messageData.put("date3", createDataItem("审批日期", time));
		messageData.put("thing6", createDataItem("申请内容", content));
		// 将订阅消息内容转换为JSON字符串
		String jsonData = messageData.toJSONString();
		WeChatSubscribeMessageSender.sendSubscribeMessage(accessToken,openid,templateId,templateUrl,jsonData);
	}

	public String refreshAccessToken(String appid, String secret){
		if(StringUtils.isEmpty(appid) || StringUtils.isEmpty(secret)){
			return "刷新微信AccessToken时，缺少appid或secret参数！";
		}
		Map<String, Object> param = new HashMap<>();
		param.put("appid",appid);
		param.put("secret",secret);
		String apiUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
		String responseBody = HttpClientUtil.doGet(apiUrl);
		JSONObject jsonObj = JSON.parseObject(responseBody);
		String accessToken = null;
		if(jsonObj != null){
			accessToken = jsonObj.getString("access_token");
		}
		return accessToken;
	}
	private static Map<String, Object> createDataItem(String name, String value) {
		Map<String, Object> item = new HashMap<>();
		item.put("value", value);
		return item;
	}
}
